//
//  CinemaViewController.m
//  豆瓣
//
//  Created by aaaaaa on 16/6/23.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CinemaViewController.h"
#import "CinemaModel.h"
#import "CinemaTableViewCell.h"

@interface CinemaViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray<CinemaModel *> *cinemaModelArray;

@end

@implementation CinemaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self config];
    [self handleData];
    [self createTableView];
}

- (void)config{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"影院";
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:86 / 255.0 green:137 / 255.0 blue:157 / 255.0 alpha:1.0];
    
}

- (void)handleData{
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"cinemalist" ofType:@"txt"];
    
    NSError *error = nil;
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:path] options:0 error:&error];
    
    NSDictionary *resultKeyOfDic = [dictionary objectForKey:@"result"];
    NSArray *dataArray = [resultKeyOfDic objectForKey:@"data"];
    
    self.cinemaModelArray = [NSMutableArray array];
    
    for (NSDictionary *dic in dataArray) {
        CinemaModel *model = [[CinemaModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [self.cinemaModelArray addObject:model];
        [model release];
    }
    
    
    
    
}


- (void)createTableView{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - 64 - 50) style:UITableViewStylePlain];
    
    tableView.delegate = self;
    tableView.dataSource = self;
    
    tableView.separatorColor = [UIColor whiteColor];
    
    [self.view addSubview:tableView];
    [tableView release];
    
    [tableView registerClass:[CinemaTableViewCell class] forCellReuseIdentifier:@"cinemaCell"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.cinemaModelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CinemaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cinemaCell"];
    
    cell.cinemaModel = self.cinemaModelArray[indexPath.row];
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
